﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClasses.Pages;
using DomainModel;

public partial class Admin_ProductCenter_OrderDetail : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitPageData();
        }
    }

    private void InitPageData()
    {
        int id = this.OrderID;
        Order order = this.LogicController.ProductManager.GetOrderByID(id);
        IEnumerable<OrderDetail> detaiList = this.LogicController.ProductManager.GetOrderDetailsByOrderID(id);

        lblAddress.Text = order.Address;
        lblCreateTime.Text = order.CreateTime.ToString();
        lblEmail.Text = order.Email;
        lblName.Text = order.CustomerName;
        lblPhone.Text = order.Phone;
        lblPostCode.Text = order.PostCode;
        lblRemark.Text = order.Remark;

        rptOrderDetail.DataSource = detaiList;
        rptOrderDetail.DataBind();
    }

    private int OrderID
    {
        get
        {
            return this.GetParam("ID", true, true, 0);
        }
    }

    private Dictionary<int, Product> m_dictProduct;

    public Dictionary<int, Product> DictProduct
    {
        get
        {
            if (m_dictProduct == null)
            {
                m_dictProduct = this.LogicController.ProductManager.GetAllProducts().ToDictionary<Product, int>(p => p.ID.Value);
            }

            return m_dictProduct;
        }
    }

    public string GetProductName(object input)
    {
        int id = Convert.ToInt32(input);
        return this.DictProduct[id].ProductName;
    }

    public string GetProductModel(object input)
    {
        int id = Convert.ToInt32(input);
        return this.DictProduct[id].Model;
    }

    public string GetProductPrice(object input)
    {
        int id = Convert.ToInt32(input);
        return this.DictProduct[id].Price.ToString("0.00");
    }

    public string GetSingleProductMoney(object productId, object number)
    {
        int count = Convert.ToInt32(number);
        return (decimal.Parse(this.GetProductPrice(productId)) * count).ToString("0.00");
    }
}
